Adds a single extension trait for &str
to unescape any backslashes. Supports no-std
.
Unescaping is designed to support as many languages as possible.
The following escapes are valid:
\\n
->\n
\\r
->\r
\\t
->\t
\\b
->\x08
\\f
->\x0C
\\'
->'
\\"
->"
\\\\
->\\
\\xNN
->\xNN
\\o
->\o
\\oo
->\oo
\\ooo
->\ooo
\\uXXXX
->\u{XXXX}
\\u{HEX}
->\u{HEX}
UTF-8 escapes specified by multiple consecutive \xNN
escapes will not work as intended, producing mojibake.
It's assumed that the escaped data is already UTF-8 encoded.
use Cow;
use UnescapeExt;
let escaped = "Hello,\\nworld!".to_unescaped;
assert_eq!;
let no_escapes = "No escapes here!".to_unescaped;
assert_eq!;
// v invalid at index 7
let invalid_escape = "Uh oh! \\xJJ".to_unescaped;
assert_eq!;